'''
Created on Jan 27, 2014

Chapter 13
'''
import sqlite3

def convert(value):
    if value.startswith('-'):
        return value.strip('-')
    if not value:
        value = '0'
    return float(value)

conn = sqlite3.connect('food.db')
curs = conn.cursor()

#curs.execute('''
#create table food (
#    id    TEXT    PRIMARY KEY,
#    desc    TEXT,
#    water    FLOAT,
#    kcal    FLOAT
#)             
#''')

query = 'INSERT INTO food values (?,?,?,?)'

for line in open('ABBREV.txt'):
    fields = line.split('^')
    vals = [convert(f) for f in fields[:4]]
    print(vals)
    curs.execute(query, vals)
    
conn.commit()
conn.close()